package com.zwxu.stack;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;

public class FlattenNestedListIteratorDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	

	}

}
class NestedInteger{
	private Integer integer;
	private List<NestedInteger> list;
	
	
	public void setInteger(Integer integer) {
		this.integer = integer;
	}

	public void setList(List<NestedInteger> list) {
		this.list = list;
	}

	public boolean isInteger(){
		if(this.getList() == null){
			return true;
		}
		return false;
	}
	
	public Integer getInteger(){
		return integer;
	}
	public List<NestedInteger> getList(){
		return list;
	}
}

class NestedIterator implements Iterator<Integer> {
    Stack<NestedInteger> stack = new Stack<NestedInteger>();
 
    public NestedIterator(List<NestedInteger> nestedList) {
        if(nestedList==null)
            return;
 
        for(int i=nestedList.size()-1; i>=0; i--){
            stack.push(nestedList.get(i));
        }
    }
 
    @Override
    public Integer next() {
        return stack.pop().getInteger();
    }
 
    @Override
    public boolean hasNext() {
        while(!stack.isEmpty()){
            NestedInteger top = stack.peek();
            if(top.isInteger()){
                return true;
            }else{
                stack.pop();
                for(int i=top.getList().size()-1; i>=0; i--){
                    stack.push(top.getList().get(i));
                }
            }
        }
 
        return false;
    }
    
    @Override
    public void remove() {}
}